package com.lyc.controller;

import com.github.pagehelper.PageInfo;
import com.lyc.pojo.Cart;
import com.lyc.pojo.Goods;
import com.lyc.service.GoodsService;
import com.lyc.service.GouService;
import com.lyc.tool.Msg1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/gou")
@CrossOrigin
public class GouController {
    @Autowired
    private GouService gouService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/select/{uid}",method = RequestMethod.GET)
    @ResponseBody//查询当前用户的购物车信息
    public List<Cart> select(@PathVariable("uid") int uid){
        List<Cart> list = gouService.select(uid);
        return list;
    }
    @ResponseBody
    @RequestMapping(value = "/add/{gid}/{uid}",method = RequestMethod.POST)//修改当前用户购物车指定商品的数量
    public List<Cart> add(@PathVariable("gid") int gid,@PathVariable("uid")int uid){
        Cart cart = gouService.selectCart(gid, uid);//当前用户购物车是否含有该商品
        if(cart!=null){
            int num=cart.getNum()+1;
            int scid=cart.getScid();
            gouService.updateone(num, scid, uid);
        }else {
            Goods goods = goodsService.selectOne(gid);
            float price=  goods.getNewPrice();
            Cart cart1 = new Cart();
            cart1.setGid(gid);
            cart1.setNum(1);
            cart1.setUid(uid);
            cart1.setPrice(price);
            gouService.addGoods(cart1);
        }
        List<Cart> cartList = gouService.select(uid);
        return cartList;
    }
    @ResponseBody
    @RequestMapping(value = "/sub/{gid}/{uid}",method = RequestMethod.POST)//修改当前用户购物车指定商品的数量
    public List<Cart> sub(@PathVariable("gid") int gid,@PathVariable("uid")int uid){
        Cart cart = gouService.selectCart(gid, uid);//当前用户购物车是否含有该商品
        if(cart!=null){
            int num1=cart.getNum()-1;
            if(num1>0){
                int scid=cart.getScid();
                gouService.updateone(num1, scid, uid);
            }
        }
        List<Cart> cartList = gouService.select(uid);
        return cartList;
    }

    @RequestMapping(value = "/remove/{scid}/{uid}",method = RequestMethod.POST)
    @ResponseBody//直接清空购物车中某类商品信息
    public List<Cart> remove(@PathVariable("scid")int scid,@PathVariable("uid")int uid){
        int i=gouService.removeByScid(scid);
        return gouService.select(uid);
    }

    /*后台*/
    @RequestMapping("/all")
    @ResponseBody
    //page当前页
    //limt 每页行数
    //PageHeler.startPage(当前页，每页行数);----------测试
    //修改sql：select * from student limit ?,?----(page-1)*limit,limit
    //总行数：select counr(*) from student
    public Msg1<Cart> selectAll(@RequestParam(defaultValue = "1")Integer page,
                                @RequestParam(defaultValue = "5")Integer limit){
        List<Cart> carts =gouService.selectAll();
        PageInfo<Cart> pageInfo=gouService.getGoByPage(page,limit);
        System.out.println(pageInfo);
        Msg1<Cart> msg=new Msg1<>();
        System.out.println(pageInfo.getList());
        //count属性类型改为long值
        msg.setCount(pageInfo.getTotal());
        msg.setCode(0);
        msg.setData(pageInfo.getList());
        return msg;
    }
}
